﻿package framework.util.mc 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.FrameLabel;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	/**
	 * MovieClip公共控制函数
	 * @author henrylee
	 */
	public class MCUtil
	{
		/**
		 * 立刻停止
		 * @param	container
		 */
		static public function stopAni(container:DisplayObjectContainer):void {
			if (container == null) return;
			if (container is MovieClip)
				(container as MovieClip).stop();
			if (container is InteractiveObject)
				(container as InteractiveObject).mouseEnabled = false;
			
			var child:DisplayObject;
			var i:int = container.numChildren;
			while (i--) {
				child = container.getChildAt(i);
				if (child is DisplayObjectContainer)
					stopAni(DisplayObjectContainer(child));
				else if (child is InteractiveObject)
					InteractiveObject(child).mouseEnabled = false;
			}
		}
		
		/**
		 * 从第一帧开始播放
		 * @param	container
		 * @param       frame
		 */
		static public function playAni(container:DisplayObjectContainer, frame:int = 1):void {
			if (container == null) return;
			if (container is MovieClip)
				MovieClip(container).gotoAndPlay(frame);
			if (container is InteractiveObject)
				InteractiveObject(container).mouseEnabled = true;
				
			var child:DisplayObject;
			var i:int = container.numChildren;
			while (i--) {
				child = container.getChildAt(i);
				
				if (child is DisplayObjectContainer)
					playAni(DisplayObjectContainer(child));
				else if (child is InteractiveObject)
					InteractiveObject(child).mouseEnabled = true;
			}
		}
		
		/**
		 * 停在帧
		 * @param	container
		 * @param	frame
		 */
		static public function gotoAndStop(container:DisplayObjectContainer, frame:int):void {
			if (container == null) return;
			if (container is MovieClip)
				MovieClip(container).gotoAndStop(frame);
			if (container is InteractiveObject)
				(container as InteractiveObject).mouseEnabled = false;
			
			var child:DisplayObject;
			var i:int = container.numChildren;
			while (i--) {
				child = container.getChildAt(i);
				if (child is DisplayObjectContainer)
					gotoAndStop(DisplayObjectContainer(child), frame);
				else if (child is InteractiveObject)
					InteractiveObject(child).mouseEnabled = false;
			}
		}
		
		
		/**
		 * 移除所有子对象
		 * @param	displayobj
		 */
		static public function removeAllChild(displayobj:DisplayObjectContainer):void
		{
			stopAni(displayobj);
			
			var num: int = displayobj.numChildren;
			while (num--)
				displayobj.removeChildAt(num);
			
			
			//var num: int = diplayobj.numChildren;
			//var child:DisplayObject;
			//while (num--)
			//{
				//child = diplayobj.removeChildAt(num);
				//if (child is MovieClip)
					//(child as MovieClip).stop();
				//
				//if (child.hasOwnProperty("graphics"))
				//{
					//if (child is Shape)
						//Shape(child).graphics.clear();
					//if (child is Sprite)
						//Sprite(child).graphics.clear();
				//}
				//
				//if (loop == true && child is DisplayObjectContainer)
					//removeAllChild(child as DisplayObjectContainer);
			//}
		}
		
		/**
		 * 通过label名称返回mc帧编号
		 * @param	mc
		 * @param	label_nam
		 * @return
		 */
		static public function getFrameByLabel(mc:MovieClip, label_nam:String):int
		{
			var labels: Array = mc.currentLabels;
			for each(var label: FrameLabel in labels) {
				if (label.name == label_nam) {
					return label.frame;
				}
			}
			return 1;
		}
	}
}